package com.mc.moring.widge;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mc.moring.R;

/**
 * @ProjectName: ZhiLiProject
 * @Package: com.xx.text.widget
 * @ClassName: DialogView
 * @Author: ANWEN
 * @CreateDate: 12/12/23 7:52 PM
 * @UpdateUser:
 * @UpdateDate: 12/12/23 7:52 PM
 * @UpdateRemark:
 * @Version:
 */
public class LoanRetentionDialog extends Dialog {

    DialogViewOnClickListener dialogViewOnClickListener;

    public LoanRetentionDialog(@NonNull Context context) {
        super(context, R.style.LoadDialogStyle1);
    }

    public LoanRetentionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected LoanRetentionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public interface DialogViewOnClickListener{
        void onClickConfirm();
        void onClickCancel();
    }

    public void setDialogViewOnClickListener(DialogViewOnClickListener dialogViewOnClickListener){
        this.dialogViewOnClickListener = dialogViewOnClickListener;
    }

    public void setMagin(Activity activity, View layoutParent, int number){
        if (layoutParent.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }else if(layoutParent.getLayoutParams() instanceof FrameLayout.LayoutParams){
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }
    }

    public static int getScreenWidth(Activity context){
        Display display = context.getWindowManager().getDefaultDisplay();	//获得默认（本地）的显示设备
        int screen_width = display.getWidth();
        return screen_width;
    }

    private boolean cancel;
    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.cancel = cancel;
        super.setCanceledOnTouchOutside(cancel);
    }

    @Override
    public void onBackPressed() {
        if (!cancel){
            return;
        }
        super.onBackPressed();
    }

    public void showToDialog(Context context) {
        View view = View.inflate(context,R.layout.dialog_loan_retention, null);
        Window window = this.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        TextView tv_abandonar = view.findViewById(R.id.tv_abandonar);
        TextView tv_seguir = view.findViewById(R.id.tv_seguir);
        FrameLayout frameLayout = view.findViewById(R.id.layoutParent);

        LoanRetentionDialog myDialog = new LoanRetentionDialog(context);
        myDialog.setMagin((Activity) context, frameLayout, 56 * 2);
        myDialog.setContentView(view);
        myDialog.show();

        tv_seguir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickConfirm();
                }
                myDialog.dismiss();
            }
        });
        tv_abandonar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialogViewOnClickListener!=null){
                    dialogViewOnClickListener.onClickCancel();
                }
                myDialog.dismiss();
            }
        });
    }
}
